module.exports = {
  /**
   * 海报购买记录
   * @url user/kh/postOrderConfig 前端调用的url参数地址
   */
  main: async (event) => {
    let {
      data = {}, userInfo, util, originalParam
    } = event;
    let {
      uniID,
      pubFun,
      vk,
      db,
      _
    } = util;
    let res = {
      code: 0,
      msg: "ok",
    };
    let couponName = "vk-coupon";
    let userName = "uni-id-users";
    let bonusSetName = "vk-bonus-set";
    let withdrawSetName = "vk-withdraw-set";
    let bonusRelateName = "vk-bonus-relate";
    let gasRelateName = "vk-gas-relate";
    let msgName = 'vk-message'
    let {
      sign_id,
      pid,
      coupon_id,
      stock,
      contract_no,
      
    } = data;

    // 减库存 改状态
    let couponRes = await db
      .collection(couponName)
      .where({
        _id: coupon_id,
      })
      .get();
    let couponInfo;
    if (couponRes.data.length > 0) {
      couponInfo = couponRes.data[0];
      await vk.baseDao.update({
          dbName: couponName,
          whereJson: {
            _id: coupon_id,
          },
          dataJson: {
            stock: couponInfo.stock - stock,
            status: couponInfo.stock - stock > 0 ? 1 : 2,
          },
        },
        event.util
      );
    }
    // 加油
    let userRes = await db
      .collection(userName)
      .where({
        _id: sign_id,
      })
      .get();
    // 购买人信息
    let userBuyInfo
    if (userRes.data.length > 0) {
      userBuyInfo = userRes.data[0];
      if (!userBuyInfo.gas_amount) {
        userBuyInfo.gas_name = '';
        userBuyInfo.gas_amount = 0; // 总油卡
        userBuyInfo.gas_origin = 0; // 充值金额
        userBuyInfo.gas_amount_back = 0; // 剩余加油分
        userBuyInfo.gas_amount_remind = 0; // 待释放加油分
      }
      let str = parseFloat(userBuyInfo.gas_origin) + stock * parseFloat(couponInfo.price) > 20000 ? 'VIP一星' :
        parseFloat(userBuyInfo.gas_origin) + stock * parseFloat(couponInfo.price) > 200000 ? 'VIP二星' : 'VIP'
      await db
        .collection(userName)
        .doc(sign_id)
        .update({
          gas_origin: parseFloat(userBuyInfo.gas_origin) + stock * parseFloat(couponInfo.price),
          gas_amount: parseFloat(userBuyInfo.gas_amount) + stock * parseFloat(couponInfo.all_price),
          gas_amount_back: parseFloat(userBuyInfo.gas_amount) + stock * parseFloat(couponInfo.all_price)-parseFloat(couponInfo.per_bonus),
          gas_amount_remind: parseFloat(couponInfo.per_bonus),
          gas_name: str
        });
      // 添加一条记录
      await vk.baseDao.add({
        dbName: msgName,
        dataJson: {
          flag: 1, // 专用于油气查询
          user_id: userBuyInfo._id,
          nickname: userBuyInfo.nickname,
          status: 1,
          amount: couponInfo.per_bonus,
          msg: '加油卡发放',
          type: 6 // 返利积分发放  2 系统消息  3 提现审核消息  4订单审核信息  5 推荐积分发放 6 油卡积分发放 7 油卡积分释放 8 油卡佣金发放
        }
      }, event.util);
    }

    // 获取分佣设置
    let bonusSetRes = await db
      .collection(bonusSetName)
      .where({
        relate_coupon_id: coupon_id,
      })
      .get();
    // 直推人信息
    let zhiTuiInfo
    // 间推人信息
    let jianTuiInfo
    if (bonusSetRes.data.length > 0) {
      let bonusSetResInfo = bonusSetRes.data[0];
      // 通过uid找pid  同过pid找pid  只找两级
      if (pid) {
        // 直推人
        let secondRes = await db
          .collection(userName)
          .where({
            _id: pid,
          })
          .get();
        if (secondRes.data.length > 0) {
          zhiTuiInfo = secondRes.data[0];
          if (zhiTuiInfo.gas_name && zhiTuiInfo.gas_name.indexOf('VIP') > -1) {
            if (!zhiTuiInfo.gas_amount) {
              zhiTuiInfo.gas_amount = 0; // 总油卡
              zhiTuiInfo.gas_amount_back = 0; // 剩余加油分
            }
            await db
              .collection(userName)
              .doc(zhiTuiInfo._id)
              .update({
                gas_amount: parseFloat(zhiTuiInfo.gas_amount) + parseFloat(stock * bonusSetResInfo.first * couponInfo.price / 100),
                gas_amount_back: parseFloat(zhiTuiInfo.gas_amount_back) + parseFloat(stock * bonusSetResInfo.first * couponInfo.price / 100),
              });
              await vk.baseDao.add({
                dbName: msgName,
                dataJson: {
                  flag: 1, // 专用于油气查询
                  user_id: zhiTuiInfo._id,
                  nickname: zhiTuiInfo.nickname,
                  status: 1,
                  amount: parseFloat(stock * bonusSetResInfo.first * couponInfo.price / 100),
                  msg: '加油卡佣金发放',
                  type: 8 // 返利积分发放  2 系统消息  3 提现审核消息  4订单审核信息  5 推荐积分发放 6 油卡积分发放 7 油卡积分释放 8 油卡佣金发放
                }
              }, event.util);
            let gap = couponInfo.day == 30 ? parseInt(couponInfo.sign_day) / 30 : couponInfo.sign_day
            await vk.baseDao.add({
              dbName: bonusRelateName,
              dataJson: {
                type: 2,
                status: 1,
                origin_id: userBuyInfo._id,
                origin_name: userBuyInfo.username || userBuyInfo.nickname,
                contract_no,
                tuijian_id: zhiTuiInfo._id,
                tuijian_name: zhiTuiInfo.username || zhiTuiInfo.nickname,
                // 10 * 1 * 4800 / 24 /100 = 20
                tuijian_per_bonus: (bonusSetResInfo.first * stock * couponInfo.price / gap) / 100,
                tuijian_start_time: vk.pubfn.timeFormat(vk.pubfn.getOffsetTime(new Date().getTime(), {
                  day: 1,
                  mode: "after", // after 之后 before 之前
                }), "yyyy-MM-dd"),
                tuijian_expire_time: vk.pubfn.timeFormat(vk.pubfn.getOffsetTime(new Date().getTime(), {
                  day: parseInt(couponInfo.sign_day),
                  mode: "after", // after 之后 before 之前
                }), "yyyy-MM-dd"),
              }
            }, event.util);

            if (zhiTuiInfo.pid) {
              // 间推人
              let thirdRes = await db
                .collection(userName)
                .where({
                  _id: zhiTuiInfo.pid,
                })
                .get();
              if (thirdRes.data.length > 0) {
                jianTuiInfo = thirdRes.data[0];
                if (!jianTuiInfo.gas_amount) {
                  jianTuiInfo.gas_amount = 0; // 总油卡
                  jianTuiInfo.gas_amount_back = 0; // 剩余加油分
                }
                await db
                  .collection(userName)
                  .doc(jianTuiInfo._id)
                  .update({
                    gas_amount: parseFloat(jianTuiInfo.gas_amount) + parseFloat(stock * bonusSetResInfo.second * couponInfo.price / 100),
                    gas_amount_back: parseFloat(jianTuiInfo.gas_amount_back) + parseFloat(stock * bonusSetResInfo.second * couponInfo.price / 100),
                  });
                  await vk.baseDao.add({
                    dbName: msgName,
                    dataJson: {
                      flag: 1, // 专用于油气查询
                      user_id: jianTuiInfo._id,
                      nickname: jianTuiInfo.nickname,
                      status: 1,
                      amount: parseFloat(stock * bonusSetResInfo.second * couponInfo.price / 100),
                      msg: '加油卡佣金发放',
                      type: 8 // 返利积分发放  2 系统消息  3 提现审核消息  4订单审核信息  5 推荐积分发放 6 油卡积分发放 7 油卡积分释放 8 油卡佣金发放
                    }
                  }, event.util);
                await vk.baseDao.add({
                  dbName: bonusRelateName,
                  dataJson: {
                    type: 2,
                    status: 1,
                    origin_id: userBuyInfo._id,
                    origin_name: userBuyInfo.username || userBuyInfo.nickname,
                    contract_no,
                    tuijian_id: jianTuiInfo._id,
                    tuijian_name: jianTuiInfo.username || jianTuiInfo.nickname,
                    tuijian_per_bonus: (bonusSetResInfo.second * stock * couponInfo.price / gap) / 100,
                    tuijian_start_time: vk.pubfn.timeFormat(vk.pubfn.getOffsetTime(new Date().getTime(), {
                      day: 1,
                      mode: "after", // after 之后 before 之前
                    }), "yyyy-MM-dd"),
                    tuijian_expire_time: vk.pubfn.timeFormat(vk.pubfn.getOffsetTime(new Date().getTime(), {
                      day: parseFloat(couponInfo.sign_day),
                      mode: "after", // after 之后 before 之前
                    }), "yyyy-MM-dd"),
                  }
                }, event.util);
              }
            }
          }
        }
      }
    }
    return res;
  },
};